<template>
    <div class="bottom-menu">
      <CheckButton class="select-all" @checkBtnClick="checkBtnClick" v-model="isSelectAll"></CheckButton>
      <span>全选</span>
      <span class="total-price">合计: ¥{{totalPrice}}</span>
      <span class="buy-product" :class="{'change': isColor}" @click="purchase">去计算({{$store.getters.itemNum}})</span>
    </div>
</template>

<script>
    import CheckButton from './CheckButton'

    export default {
        name: 'BottomBar',
        components: {
            CheckButton
        },
        methods: {
            checkBtnClick() {
                // 1、 判断是否有未选择的按钮
                let isSelectAll = this.$store.state.cartList.find(item => !item.checked);
                if (isSelectAll) {
                    this.$store.state.cartList.forEach(item => {
                        item.checked = true;
                    });
                }
                else {
                    this.$store.state.cartList.forEach(item => {
                    item.checked = false;
                    });
                }
            },
            purchase() {
                if (!this.isColor) {
                    console.log('购买成功！')
                }
            }
        },
        computed: {
            totalPrice() {
                const cartList = this.$store.state.cartList;
                return cartList.reduce((pre, cur) => {
                    if (cur.checked) {
                        return pre = pre + cur.count * cur.newPrice;
                    }
                    else {
                        return pre;
                    }
                }, 0);
            },
            isColor() {
                return this.$store.getters.itemNum <= 0;
            },
            isSelectAll() {
                return this.$store.state.cartList.every(item => item.checked);
            }
        }
    }
</script>

<style scoped>
  .bottom-menu {
    width: 100%;
    height: 44px;
    background-color: #eee;
    position: fixed;
    bottom: 50px;
    left: 0;
    box-shadow: 0 -2px 3px rgba(0, 0, 0, .2);
    font-size: 14px;
    color: #888;
    line-height: 44px;
    padding-left: 35px;
    box-sizing: border-box;
  }

  .bottom-menu .select-all {
    position: absolute;
    line-height: 0;
    left: 12px;
    top: 13px;
  }

  .bottom-menu .total-price {
    margin-left: 15px;
    font-size: 16px;
    color: #666;
  }

  .bottom-menu .buy-product {
    background-color: orangered;
    color: #fff;
    width: 100px;
    height: 44px;
    text-align: center;
    line-height: 44px;
    float: right;
  }

  .bottom-menu .change {
      background-color: #888;
  }
</style>